package com.chris.core.socket.netty.dto.protocol;

import com.chris.core.socket.netty.comm.ByteUtil;
import com.chris.core.socket.netty.dto.SocketParameterEnum;
import io.netty.util.CharsetUtil;

/**
 * 自定义数据结构
 */
public class CustomMessageProtocol {

    /**
     * 数据包类型
     *  SocketParameterEnum (对应枚举常数）
     * @see SocketParameterEnum
     */
    private int type;
    /**
     * 数据包长度
     */
    private int len;
    /**
     * 数据包内容
     */
    private byte[] content;

    public int getLen() {
        return len;
    }


    public int getType() {
        return type;
    }

    public SocketParameterEnum getEnumType() {
        return SocketParameterEnum.get(this.type);
    }

    /**
     * 自定义类型
     * @param content
     * @param type
     */
    public void setContent(byte[] content,SocketParameterEnum type) {
        this.len=content.length;
        this.content = content;
        this.type=type.getCode();
    }

    /**
     * 自定义类型
     * @param content
     * @param type
     */
    public void setContent(byte[] content,int type) {
        this.len=content.length;
        this.content = content;
        this.type=type;
    }

    /**
     * 字节码类型
     * @param content
     */
    public void setContent(byte[] content) {
        this.len=content.length;
        this.content = content;
        this.type=SocketParameterEnum.ByteType.getCode();
    }


    /**
     * 文本类型（utf-8)
     * @param ob
     */
    public void setContent(String ob) {
        this.content = ob.getBytes(CharsetUtil.UTF_8);
        this.type=SocketParameterEnum.StringType.getCode();
        this.len = content.length;
    }

    /**
     * 系统命令(文本utf-8)
     * @param ob
     */
    public void setContentSystemCmd(String ob) {
        this.content = ob.getBytes(CharsetUtil.UTF_8);
        this.type=SocketParameterEnum.SystemCmdType.getCode();
        this.len = content.length;
    }

    /**
     * 系统命（CmdMessageProtocol）
     * @param cmp
     * @throws Exception
     */
    public void setContentSystemCmdObject(CmdObjectProtocol cmp) throws Exception {
        this.content=ByteUtil.ObjectToBytes(cmp);
        this.type=SocketParameterEnum.SystemCmdObjectType.getCode();
        this.len=content.length;
    }

    /**
     * 文件对象（FileMessageProtocol）
     * @param fmp
     * @throws Exception
     */
    public void setContentFileObject(FileMessageProtocol fmp) throws Exception {
        this.content=ByteUtil.ObjectToBytes(fmp);
        this.type=SocketParameterEnum.FileObjectType.getCode();
        this.len=content.length;
    }

    /**
     * JSON(文本utf-8)
     * @param ob
     */
    public void setContentJson(String ob) {
        this.content = ob.getBytes(CharsetUtil.UTF_8);
        this.type=SocketParameterEnum.JsonType.getCode();
        this.len = content.length;
    }

    /**
     * XML(文本utf-8)
     * @param ob
     */
    public void setContentXml(String ob) {
        this.content = ob.getBytes(CharsetUtil.UTF_8);
        this.type=SocketParameterEnum.XmlType.getCode();
        this.len = content.length;
    }




    /**
     * int 类型
     * @param ob
     */
    public void setContent(int ob)
    {
        this.type=SocketParameterEnum.IntType.getCode();
        this.content= ByteUtil.IntToByte(ob);
        this.len=content.length;
    }

    /**
     * long 类型
     * @param x
     */
    public void setContent(long x) {
        this.type=SocketParameterEnum.LongType.getCode();
        this.content=ByteUtil.LongToByte(x);
        this.len=content.length;
    }

    /**
     * float 类型
     * @param x
     */
    public void setContent(float x) {
        this.type=SocketParameterEnum.FloatType.getCode();
        this.content=ByteUtil.FloatToByte(x);
        this.len=content.length;
    }

    /**
     * 浮点类型
     * @param x
     */
    public void setContent(double x) {
        this.type=SocketParameterEnum.DoubleType.getCode();
        this.content=ByteUtil.DoubleToByte(x);
        this.len=content.length;
    }



    /**
     * 获取字节码content
     * @return
     */
    public byte[] getByteContent()
    {
        return this.content;
    }

    /**
     * 获取转换后Content对象
     * @return
     */
    public Object getObjectContent() throws Exception {
        if (type==SocketParameterEnum.DoubleType.getCode())
        {
            return ByteUtil.ByteToDouble(this.content);
        }
        else if (type==SocketParameterEnum.FloatType.getCode())
        {
            return ByteUtil.ByteToFloat(this.content);
        }
        else if (type==SocketParameterEnum.LongType.getCode())
        {
            return ByteUtil.ByteToLong(this.content);
        }
        else if (type==SocketParameterEnum.IntType.getCode())
        {
            return ByteUtil.ByteToInt(this.content);
        }
        else if (type==SocketParameterEnum.StringType.getCode() || type==SocketParameterEnum.SystemCmdType.getCode()  || type==SocketParameterEnum.JsonType.getCode()  || type==SocketParameterEnum.XmlType.getCode())
        {
            //普通文本 、 系统命令 、XML 、 JSON
            return ByteUtil.ByteToString(this.content);
        }
        else if (type==SocketParameterEnum.ByteType.getCode())
        {
            //单纯字节码类型
            return this.content;
        }
        else if (type==SocketParameterEnum.SystemCmdObjectType.getCode())
        {
            //object命令->文件对象类型
            return ByteUtil.BytesToObject(this.content);
        }
        else if (type==SocketParameterEnum.FileObjectType.getCode())
        {
            //文件字节码->文件对象类型
            return ByteUtil.BytesToObject(this.content);
        }
        else if (type==SocketParameterEnum.ObjectType.getCode())
        {
            //文件字节码-对象类型
            return ByteUtil.BytesToObject(this.content);
        }
        else
            return this.content;
    }


}
